﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using PrepaidCard.Models;
using Tools;
using Tools.Net;

namespace PrepaidCard
{
    public class Query
    {
        public static int index { get; set; }

        [DllImport("AntiVC.dll")]
        public static extern int LoadCdsFromFile(string FilePath);

        [DllImport("AntiVC.dll")]
        public static extern int LoadCdsFromBuffer(byte[] FileBuffer, int FileBufLen);

        [DllImport("AntiVC.dll")]
        public static extern bool GetVcodeFromFile(int CdsFileIndex, string FilePath, StringBuilder Vcode);

        [DllImport("AntiVC.dll")]
        public static extern bool GetVcodeFromBuffer(int CdsFileIndex, byte[] FileBuffer, int ImgBufLen, StringBuilder Vcode);

        [DllImport("AntiVC.dll")]
        public static extern bool GetVcodeFromURL(int CdsFileIndex, string ImgURL, StringBuilder Vcode);

        [DllImport("urlmon.dll", EntryPoint = "URLDownloadToFileA")]
        public static extern int URLDownloadToFile(int pCaller, string szURL, string szFileName, int dwReserved, int lpfnCB);


        public static Status QStatus10000(string number)
        {
            string html = "";
            string p = string.Format(@"callCount=1
page=/bill/bill.htm?id=6
httpSessionId=0000c2O0Yjle9pHR5qKpohnnZfz:-1
scriptSessionId=2D6D7B65E2482B0D97BA51D2B3C4E143964
c0-scriptName=billDwr
c0-methodName=queryCard
c0-id=0
c0-param0=string:{0}
batchId=1", number);
            const string url = "http://cq.189.cn/bill/dwr/call/plaincall/billDwr.queryCard.dwr";
            Http.FormSubmit(url, out html, method: "post", parmeter: p);
            Status status = null;
            try
            {
                var jsonText = Often.SpecifiedValueNoreg(html, "('1','0',", ");");
                status = new Status();
                status.callingBumber = Often.SpecifiedValueNoreg(html, "callingBumber:" + '"', '"'.ToString());
                status.cardNo = Often.SpecifiedValueNoreg(html, "cardNo:" + '"', '"'.ToString());
                status.expirationTime = Often.SpecifiedValueNoreg(html, "expirationTime:" + '"', '"'.ToString());
                status.statusDesc = Often.SpecifiedValueNoreg(html, "statusDesc:" + '"', '"'.ToString());

                return status;
            }
            catch (Exception)
            {
                return null;
            }

        }

        public static Status QStatus10010(string number)
        {
            string html = "";
            string p = "";
            string imgCode = "";
            string url = "http://info.10010.com/database/card/queryCard.do";
            const string imgURL = "http://info.10010.com/database/sinova.do?width=72&height=25";
            string imgPath = Environment.CurrentDirectory + "\\t10010.bmp";
            var result = new StringBuilder('\0', 256);
            var status = new Status();
            int rCount = 0;
            var cookie = Http.FormSubmit(url, out html);
        reTry:
            try
            {
                rCount++;
                var bmp = Http.GetWebBmp(imgURL, cookie);
                byte[] buffer;
                int fileLen;
                lock (typeof(Query))
                {
                    bmp.Save(imgPath);

                    FileStream fsMyfile = File.OpenRead(imgPath);
                    fileLen = (int)fsMyfile.Length;
                    buffer = new byte[fileLen];
                    fsMyfile.Read(buffer, 0, fileLen);
                    fsMyfile.Close();
                }

                if (GetVcodeFromBuffer(index, buffer, fileLen, result))
                {
                    imgCode = result.ToString();

                    url = string.Format(
                        "http://info.10010.com/database/card/queryCard.do?cardPassword={0}&imgCode={1}", number, imgCode);
                    Http.FormSubmit(url, out html, method: "post", parmeter: p, cookieContainer: cookie);
                    if (html.IndexOf("验证码不正确") != -1)
                    {
                        if (rCount < 5) goto reTry;
                    }
                    html = Often.SpecifiedValue(html, "充值卡状态：  </td>", "</td>", '"'.ToString());
                    status.statusDesc = Often.MoveHtml(html);
                    if (status.statusDesc.Equals(""))
                    {
                        goto reTry;
                    }
                }
                else
                {
                    if (rCount < 5) goto reTry;
                }
            }
            catch
            {
                if (rCount < 3) goto reTry;
            }
            return status;
        }
    }
}
